import React, { useState, useEffect } from 'react';
import SelfHead from 'components/common/selfHead';
import Cascade from 'components/common/cascade';
import { List, Toast, TextareaItem } from 'antd-mobile';
import { createForm } from 'rc-form';
import SelfInput from 'components/common/selfInput';
import Star from 'components/common/star';
import { updateCompanyInfo } from 'ser/actions';
import industryTypes from 'dic/industryType';
import companyPropertys from 'dic/companyProperty';
import { history } from 'umi';
import getYears from 'utils/getYears';

function UpdateInfo(props) {
  const user = JSON.parse(sessionStorage.getItem('user'));
  const [flag, setFlag] = useState(false);
  const [obj, setObj] = useState({
    industryTypes: industryTypes,
    companyPropertys: companyPropertys,
    industryType: user.companyType,
    companyName: user.companyName,
    establishmentTime: '',
    registerMoney: '', //注册资金
    turnover: '', //营业额
    employeeQuantity: '', //员工数量
    briefIntroduction: '', //公司简介
    companyProperty: '', //企业性质
    address: '',
    linkman: '',
    linkphone: '',
  });

  function handleOnChange(v, label, field) {
    if (v instanceof Array) {
      v = v[0];
    }
    setObj({
      ...obj,
      [field]: v,
    });
  }
  async function handleSubmit() {
    if (flag) {
      Toast.info('不可重复提交', 1);
      return;
    }
    if (Object.values(obj).some(item => item === '')) {
      console.log(Object.values(obj));
      Toast.info('请填写完整信息', 1);
      return;
    }
    setFlag(true);
    const res = await updateCompanyInfo({
      ...obj,
      accountName: user.username,
    });
    if (res.code === 0) {
      sessionStorage.setItem('isComplete', true);
      history.goBack();
    } else {
      setFlag(false);
    }
  }
  return (
    <div className="info">
      <SelfHead title="完善企业资料" />
      <div className="info-body_form">
        <div className="info-title">
          <Star />
          企业基本信息
        </div>
        <List className="self-list">
          <Cascade
            label="行业类型"
            title="请选择"
            data={industryTypes}
            cols={1}
            handleOnChange={handleOnChange}
            defaultValue={[obj.industryType]}
            field="industryType"
            isRequired={false}
            disabled
          />
          <SelfInput
            label="企业名称"
            rule={{
              len: 3,
              empty: '请输入企业名称',
              error: '请正确输入企业名称',
            }}
            handleInputChange={handleOnChange}
            field="companyName"
            defaultValue={obj.companyName}
            placeholder="请输入企业名称"
            isRequired={false}
            disabled
          />
          <Cascade
            label="成立时间"
            data={getYears()}
            cols={1}
            handleOnChange={handleOnChange}
            field="establishmentTime"
            isRequired={false}
          />
          <SelfInput
            label="注册资金"
            rule={{
              len: 0,
              empty: '请输入注册资金',
            }}
            handleInputChange={handleOnChange}
            field="registerMoney"
            unit="万元"
            isRequired={false}
            type="digit"
          />
          <SelfInput
            label="营业额"
            rule={{
              len: 0,
              empty: '请输入营业额',
            }}
            handleInputChange={handleOnChange}
            field="turnover"
            unit="万元"
            isRequired={false}
            type="digit"
          />
          <SelfInput
            label="员工数量"
            rule={{
              len: 0,
              empty: '请输入员工数量',
            }}
            handleInputChange={handleOnChange}
            field="employeeQuantity"
            unit="人"
            isRequired={false}
            type="digit"
          />
          <Cascade
            label="企业性质"
            title="请选择"
            data={companyPropertys}
            cols={1}
            handleOnChange={handleOnChange}
            field="companyProperty"
            isRequired={false}
          />
          <SelfInput
            label="公司简介"
            type="textarea"
            rule={{
              len: 0,
              empty: '请输入公司简介',
            }}
            handleInputChange={handleOnChange}
            field="briefIntroduction"
            isRequired={false}
            placeholder=""
          />
        </List>
        <div className="info-title">
          <Star />
          企业联系信息
        </div>
        <List className="self-list">
          <SelfInput
            label="单位地址"
            rule={{
              len: 2,
              empty: '请输入单位地址',
              error: '请正确输入单位地址',
            }}
            handleInputChange={handleOnChange}
            field="address"
            placeholder="请输入单位地址"
            isRequired={false}
          />
          <SelfInput
            label="联系人"
            rule={{
              len: 2,
              empty: '请输入联系人',
              error: '请正确输入联系人',
            }}
            handleInputChange={handleOnChange}
            field="linkman"
            placeholder="请输入联系人"
            isRequired={false}
          />
          <SelfInput
            label="联系电话"
            rule={{
              len: 5,
              empty: '请输入联系电话',
            }}
            handleInputChange={handleOnChange}
            field="linkphone"
            placeholder="请输入联系电话"
            isRequired={false}
          />
        </List>
      </div>
      <div className="supply-add-btn" onClick={handleSubmit}>
        提交
      </div>
    </div>
  );
}
export default createForm()(UpdateInfo);
